package cn.tedu.client.controller;


import cn.tedu.base.response.JsonResult;
import cn.tedu.base.response.StatusCode;
import cn.tedu.client.constant.StatusConstant;
import cn.tedu.client.dao.mapper.ClientMapper;
import cn.tedu.client.pojo.dto.ClientRegisterDTO;
import cn.tedu.client.pojo.dto.ClientUpdateDTO;
import cn.tedu.client.pojo.vo.ReceiverVO;
import cn.tedu.client.service.ClientService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
//import org.springframework.http.server.reactive.ServerHttpRequest;
//import org.springframework.web.server.ServerWebExchange;
//import springfox.documentation.spring.web.json.Json;
//import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestController
@RequestMapping("/font/client")
public class ClientController {

    @Autowired
    private ClientService clientService;
    @Autowired
    private ClientMapper clientMapper;

    /*
     * clientRegisterDTO接收用户写入的信息
     * */
    @PostMapping("register")
    public JsonResult register(@RequestBody ClientRegisterDTO clientRegisterDTO) {
        log.debug("用户输入信息clientRegisterDTO:{}", clientRegisterDTO);
        String resultMsg = clientService.register(clientRegisterDTO);
        if (resultMsg.equals(StatusConstant.REGISTER_FAIL)) {
            return new JsonResult(StatusCode.OPERATION_FAILED);
        } else if (resultMsg.equals(StatusConstant.PHONENUM_ISEXIST)) {
            return new JsonResult(StatusCode.PHONENUM_ALREADY_EXISTS);
        } else {
            return new JsonResult(StatusCode.REGISTER_SUCCESS);
        }
    }

//    @GetMapping("loadInfo")
//    public JsonResult loadInfo(HttpServletRequest request) {
//        return clientService.loadInfo(request);
//    }

    @GetMapping("/loadInfo/{userId}")
    public JsonResult loadInfo(@PathVariable Long userId) {
        log.debug("userId:{}",userId);
        return clientService.loadInfo(userId);
    }

    @PostMapping("update")
    public JsonResult update(@RequestBody ClientUpdateDTO clientUpdateDTO){
        log.debug("ClientUpdateDTO:{}",clientUpdateDTO);
        return clientService.update(clientUpdateDTO);
    }

    @GetMapping("getReceiver/{userId}")
    public JsonResult getReceiver(@PathVariable Integer userId){
        log.debug("控制器接收到收款方id:{}",userId);
        ReceiverVO receiverVO = clientService.getReceiver(userId);
        return new JsonResult(StatusCode.SUCCESS,receiverVO);
    }

}
